/*
* @(#)ListUserGroupAction.java
*
* Copyright (c) 2003 DCIVision Ltd
* All rights reserved.
*
* This software is the confidential and proprietary information of DCIVision
* Ltd ("Confidential Information").  You shall not disclose such Confidential
* Information and shall use it only in accordance with the terms of the license
* agreement you entered into with DCIVision Ltd.
*/
package com.dcivision.user.web;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionMapping;

import com.dcivision.dms.dao.DmsObjectHomeFolderDAObject;
import com.dcivision.dms.dao.DmsPersonalStorageSetupDAObject;
import com.dcivision.framework.ApplicationException;
import com.dcivision.framework.SessionContainer;
import com.dcivision.framework.SystemFunctionConstant;
import com.dcivision.framework.TextUtility;
import com.dcivision.framework.Utility;
import com.dcivision.framework.web.AbstractListAction;
import com.dcivision.framework.web.AbstractSearchForm;
import com.dcivision.user.bean.UserGroup;
import com.dcivision.user.bean.UserMember;
import com.dcivision.user.dao.MtmUserGroupUserRoleDAObject;
import com.dcivision.user.dao.MtmUserRecordUserGroupDAObject;
import com.dcivision.user.dao.UserGroupDAObject;
import com.dcivision.user.dao.UserRoleDAObject;
import com.dcivision.workflow.dao.MtmWorkflowDynamicUserActorDAObject;
import com.dcivision.workflow.dao.MtmWorkflowStepUserActorDAObject;


/**
 * <p>Class Name:       ListUserGroupAction.java    </p>
 * <p>Description:      The list action class for ListUserGroup.jsp</p>
 *
 *    @author           Rollo Chan
 *    @company          DCIVision Limited
 *    @creation date    24/07/2003
 *    @version          $Revision: 1.11 $
 */

public class ListUserGroupAction extends AbstractListAction {

  public static final String REVISION = "$Revision: 1.11 $";


  public final static String VAR_USER_ROLE_LIST = "varUserRoleList";


  /**
   *  Constructor - Creates a new instance of ListUserGroupAction and define the default listName.
   */
  public ListUserGroupAction() {
    super();
    this.setListName("userGroupList");
  }

  /**
   * getMajorDAOClassName
   *
   * @return  The class name of the major DAObject will be used in this action.
   */
  public String getMajorDAOClassName() {
    return("com.dcivision.user.dao.UserGroupDAObject");
  }

  /**
   * getFunctionCode
   *
   * @return  The corresponding system function code of action.
   */
  public String getFunctionCode() {
    return(SystemFunctionConstant.SETUP_USER_GROUP);
  }

  /**
   * getListData
   *
   * Override the parent's function. Since it required to get the user role
   *
   * @param mapping
   * @param form
   * @param request
   * @param response
   * @throws ApplicationException
   */
  public void getListData(ActionMapping mapping, AbstractSearchForm form, HttpServletRequest request, HttpServletResponse response) throws ApplicationException {
    super.getListData(mapping, form, request, response);

    // Get the user roles of each user record.
    List listResult = (ArrayList)request.getAttribute(this.getListName());
    List listRole = new ArrayList();
    UserRoleDAObject userRoleDAO = new UserRoleDAObject(this.getSessionContainer(request), this.getConnection(request));
    for (int i=0; i<listResult.size(); i++) {
      UserGroup userGroup = (UserGroup)listResult.get(i);
      listRole.add(userRoleDAO.getListByUserGroupID(userGroup.getID()));
    }
    request.setAttribute(VAR_USER_ROLE_LIST, listRole);
  }
  
  /**
   *  Method deleteListData() - delete the item(s) from listing page
   *
   *  @param      mapping               ActionMapping from struts
   *  @param      form                  ActionForm from struts
   *  @param      request               HttpServletReuqest
   *  @param      respond               HttpServletRespond
   *  @return     void
   *  @throws     ApplicationException  Throws ApplicationException the deleteion failed
   */
  public void deleteListData(ActionMapping mapping, AbstractSearchForm form, HttpServletRequest request, HttpServletResponse response) throws ApplicationException {
    //simon_add_S
	SessionContainer sessionContainer=this.getSessionContainer(request);
	Connection conn=this.getConnection(request);
	String[] basicSelectID = form.getBasicSelectedID();
	
	if (!Utility.isEmpty(basicSelectID)){
		UserGroupDAObject userGroupDAO=new UserGroupDAObject(sessionContainer,conn);
		MtmWorkflowStepUserActorDAObject mtmWorkflowStepActorDAO=new MtmWorkflowStepUserActorDAObject(sessionContainer,conn);
		MtmWorkflowDynamicUserActorDAObject dynamicUserActorDAO=new MtmWorkflowDynamicUserActorDAObject(sessionContainer,conn);
		String[] tmpBasicSelectID = new String[basicSelectID.length];
  		int tmpBasicSelectIndex = 0;
  		
		for(int i=0;i<basicSelectID.length;i++){
			if(Utility.isEmpty(basicSelectID[i])){
				continue;
			}
			List userGroupList=mtmWorkflowStepActorDAO.getListWorkflowStepIDByActorIDAndActorType(TextUtility.parseIntegerObj(basicSelectID[i]),UserMember.MEMBER_TYPE_GROUP);
  			List dynamicUserGroupList=dynamicUserActorDAO.getListWorkflowStepIDByActorIDAndActorType(TextUtility.parseIntegerObj(basicSelectID[i]),UserMember.MEMBER_TYPE_GROUP);			
			if(Utility.isEmpty(userGroupList) && Utility.isEmpty(dynamicUserGroupList)){
				tmpBasicSelectID[tmpBasicSelectIndex] = basicSelectID[i];
  				++tmpBasicSelectIndex;
			} else {
  				Integer userGroupRecordID=TextUtility.parseIntegerObj(basicSelectID[i]);
  				UserGroup userGroupRecord = (UserGroup)userGroupDAO.getObjectByID(userGroupRecordID);
  				this.addError(request, "common.message.records_not_delete", userGroupRecord.getGroupName());
  			}
		}
		
		if (tmpBasicSelectIndex > 0){
  			String[] tmp = new String[tmpBasicSelectIndex];
  			for (int m = 0; m < tmpBasicSelectIndex; m++){
    			tmp[m] = tmpBasicSelectID[m]; 
    			form.setBasicSelectedID(tmp);
    		}
  		} else {
  			form.setBasicSelectedID(null);
  		}
	}
	//simon_add_E
	if (!Utility.isEmpty(form.getBasicSelectedID())) {//simon_add
		super.deleteListData(mapping,form,request,response);
	    //SessionContainer sessionContainer=this.getSessionContainer(request);//simon_del
	    //Connection conn=this.getConnection(request);//simon_del
	    DmsObjectHomeFolderDAObject dmsObjectHomeFolderDAO=new DmsObjectHomeFolderDAObject(sessionContainer,conn);
	    DmsPersonalStorageSetupDAObject dmsPersonalStorageSetupDAO=new DmsPersonalStorageSetupDAObject(sessionContainer,conn);
	    for(int i=0;i<form.getBasicSelectedID().length;i++){
	      Integer groupID=TextUtility.parseIntegerObj(form.getBasicSelectedID()[i]);
	      dmsObjectHomeFolderDAO.deleteByMemberIDMemberType(groupID,UserMember.MEMBER_TYPE_GROUP);
	      dmsPersonalStorageSetupDAO.deleteByMemberIDMemberType(groupID,UserMember.MEMBER_TYPE_GROUP);
	
	      MtmUserGroupUserRoleDAObject mtmUserGroupUserRoleDAO = new MtmUserGroupUserRoleDAObject(this.getSessionContainer(request), this.getConnection(request));
	      // Delete the old user roles belonged to the user group
	      mtmUserGroupUserRoleDAO.deleteListByUserGroupID(groupID);
	
	      MtmUserRecordUserGroupDAObject mtmUserRecordUserGroupDAO = new MtmUserRecordUserGroupDAObject(this.getSessionContainer(request), this.getConnection(request));
	      // Delete the old user groups belonged to the user
	      mtmUserRecordUserGroupDAO.deleteListByUserGroupID(groupID);
	    }
	}//simon_add
  }
}
